package vue;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;

import model.Model;

public class Fenetre extends JFrame {

	
	private static final int HAUTEUR_INTERFACE_MINIMAL_LIGNE_1 = 20;
	private static final int HAUTEUR_INTERFACE_MINIMAL_LIGNE_2 = 60;
	private static final int HAUTEUR_INTERFACE_MINIMAL = HAUTEUR_INTERFACE_MINIMAL_LIGNE_1+HAUTEUR_INTERFACE_MINIMAL_LIGNE_2;
	private static final int HAUTEUR_INTERFACE_MINIMAL_COL_1 = 110;
	private static final int LARGEUR_INTERFACE_MINIMAL_GAUCHE = 200;
	

	private ImageIcon icone(String name){
		return new ImageIcon(Fenetre.class.getResource("Resources"+File.separator+name+".png"));
	}
	
	private Model model;

	public Fenetre(Model model) {
		this.model = model;
		JPanel panelBoutonsControleInterfaceMinimalLigne1 = new JPanel();
		panelBoutonsControleInterfaceMinimalLigne1.setLayout(new BoxLayout(
				panelBoutonsControleInterfaceMinimalLigne1, BoxLayout.X_AXIS));
		panelBoutonsControleInterfaceMinimalLigne1.add(new JButton(icone("backward_big")));
		panelBoutonsControleInterfaceMinimalLigne1.add(new JButton(icone("play_big")));
		panelBoutonsControleInterfaceMinimalLigne1.add(new JButton(icone("forward_big")));
		

		
		JPanel panelBoutonsControleInterfaceMinimalLigne2 = new JPanel();
		panelBoutonsControleInterfaceMinimalLigne2.setLayout(new BoxLayout(
				panelBoutonsControleInterfaceMinimalLigne2, BoxLayout.X_AXIS));

		panelBoutonsControleInterfaceMinimalLigne2.add(new JButton(icone("previous_graphite")));
		panelBoutonsControleInterfaceMinimalLigne2.add(new JButton(icone("stop_graphite")));
		panelBoutonsControleInterfaceMinimalLigne2.add(new JButton(icone("next_graphite")));

		JPanel panelBoutonsControleInterfaceMinimal = new JPanel();
		panelBoutonsControleInterfaceMinimal.setLayout(new BoxLayout(
				panelBoutonsControleInterfaceMinimal, BoxLayout.Y_AXIS));
		panelBoutonsControleInterfaceMinimal
				.add(panelBoutonsControleInterfaceMinimalLigne1);
//		panelBoutonsControleInterfaceMinimal
//		.add(Box.createRigidArea(new Dimension(20,20)));
		panelBoutonsControleInterfaceMinimal
				.add(panelBoutonsControleInterfaceMinimalLigne2);
		
		panelBoutonsControleInterfaceMinimal.setMinimumSize(new Dimension(LARGEUR_INTERFACE_MINIMAL_GAUCHE, HAUTEUR_INTERFACE_MINIMAL));
	
		

		JPanel panelPrincipalHautTitreInterfaceMinimal = new JPanel();
		panelPrincipalHautTitreInterfaceMinimal.setLayout(new BoxLayout(
				panelPrincipalHautTitreInterfaceMinimal, BoxLayout.X_AXIS));
		panelPrincipalHautTitreInterfaceMinimal.add(new JLabel("Lecteur VLC"));
		panelPrincipalHautTitreInterfaceMinimal.add(Box.createHorizontalGlue());
		panelPrincipalHautTitreInterfaceMinimal.add(new JLabel("00:00"));

		JSlider slider = new JSlider();
		slider.setEnabled(false);
		slider.setValue(0);

		JPanel panelPrincipalHautAvancementInterfaceMinimal = new JPanel();
		panelPrincipalHautAvancementInterfaceMinimal
				.setLayout(new BoxLayout(
						panelPrincipalHautAvancementInterfaceMinimal,
						BoxLayout.X_AXIS));
		panelPrincipalHautAvancementInterfaceMinimal.add(slider);

		JPanel panelPrincipalHautInterfaceMinimal = new JPanel();
		panelPrincipalHautInterfaceMinimal.setLayout(new BoxLayout(
				panelPrincipalHautInterfaceMinimal, BoxLayout.Y_AXIS));
		panelPrincipalHautInterfaceMinimal
				.add(panelPrincipalHautTitreInterfaceMinimal);
		panelPrincipalHautInterfaceMinimal
				.add(panelPrincipalHautAvancementInterfaceMinimal);


		JSlider slider2 = new JSlider();
		slider2.setPreferredSize(new Dimension(120, 40));
		slider2.setMaximumSize(new Dimension(120, 40));

		JPanel panelPrincipalBasInterfaceMinimal = new JPanel();
		panelPrincipalBasInterfaceMinimal.setLayout(new BoxLayout(
				panelPrincipalBasInterfaceMinimal, BoxLayout.X_AXIS));
		// panelPrincipalBasInterfaceMinimal
		// .setPreferredSize(new Dimension(0,
		// Fenetre.HAUTEUR_INTERFACE_MINIMAL_LIGNE_2));
		panelPrincipalBasInterfaceMinimal.add(new JLabel(icone("volume_low")));
		panelPrincipalBasInterfaceMinimal.add(slider2);
		panelPrincipalBasInterfaceMinimal.add(new JLabel(icone("volume_high")));
		panelPrincipalBasInterfaceMinimal.add(Box.createHorizontalGlue());
		panelPrincipalBasInterfaceMinimal.add(new JButton(icone("equalizerdrawer_graphite")));
		panelPrincipalBasInterfaceMinimal.add(new JButton(icone("playlistdrawer_active")));

		JPanel panelPrincipalInterfaceMinimal = new JPanel();
		panelPrincipalInterfaceMinimal.setLayout(new BoxLayout(
				panelPrincipalInterfaceMinimal, BoxLayout.Y_AXIS));
		panelPrincipalInterfaceMinimal.add(panelPrincipalHautInterfaceMinimal);
		panelPrincipalInterfaceMinimal.add(panelPrincipalBasInterfaceMinimal);

		
		
		JPanel panelInterfaceMinimal = new JPanel();
		panelInterfaceMinimal.setLayout(new BorderLayout());
		// panelInterfaceMinimal.setBackground(Color.red);
		panelInterfaceMinimal.add(panelBoutonsControleInterfaceMinimal,
				BorderLayout.LINE_START);
		panelInterfaceMinimal.add(panelPrincipalInterfaceMinimal,
				BorderLayout.CENTER);

		JPanel panelInterfaceEtenduePlayList = new JPanel();
		panelInterfaceEtenduePlayList.add(new JLabel("playlist"));

		JTextField textFieldRecherche = new JTextField(8);
		// taille min est contrainte par la taille du textField, donc le 15
		// n'est pas visible en pratique
		textFieldRecherche.setMaximumSize(new Dimension(15, 999));

		JPanel panelInterfaceEtendueBoutons = new JPanel();
		panelInterfaceEtendueBoutons.setLayout(new BoxLayout(
				panelInterfaceEtendueBoutons, BoxLayout.X_AXIS));
		panelInterfaceEtendueBoutons.add(new JButton(icone("add_embedded")));
		panelInterfaceEtendueBoutons.add(new JButton(icone("shuffle_embedded")));
		panelInterfaceEtendueBoutons.add(new JButton(icone("repeat_embedded")));
		panelInterfaceEtendueBoutons.add(Box.createHorizontalGlue());
		panelInterfaceEtendueBoutons.add(new JLabel("15 éléments"));
		panelInterfaceEtendueBoutons.add(Box.createHorizontalGlue());
		panelInterfaceEtendueBoutons.add(new JLabel("?"));
		panelInterfaceEtendueBoutons.add(textFieldRecherche);

		JPanel panelInterfaceEtendue = new JPanel();
		panelInterfaceEtendue.setLayout(new BorderLayout());
		panelInterfaceEtendue.add(panelInterfaceEtenduePlayList,
				BorderLayout.CENTER);
		panelInterfaceEtendue.add(panelInterfaceEtendueBoutons,
				BorderLayout.PAGE_END);

		this.setLayout(new BorderLayout());
		this.add(panelInterfaceMinimal, BorderLayout.PAGE_START);
		this.add(panelInterfaceEtendue, BorderLayout.CENTER);
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			};
			
		});
			

		this.addComponentListener(new ComponentAdapter(){
			public void componentResized(ComponentEvent e){
				System.out.println("fenêtre redimensionnée : " + getSize().toString());
			}
		});


		this.pack();
		this.setVisible(true);
		this.setMinimumSize(panelInterfaceMinimal.getSize());
		
		this.setSize(500,250);

	}

}
